package com.zf.shuashua.manager;

import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.webkit.WebView;
import android.widget.TextView;

import com.aliyun.common.utils.DensityUtil;
import com.bytedance.sdk.openadsdk.core.widget.webview.SSWebView;
import com.zf.shuashua.R;
import com.zf.shuashua.advert.MistakeConfigManager;
import com.zf.shuashua.api.Store;

import java.util.HashMap;
import java.util.Random;

public class AdAutoTouchManager {

    private static AdAutoTouchManager instance;

    private HashMap<Integer, Integer> hashArray = new HashMap<>();

    private AdAutoTouchManager() {
    }

    public static AdAutoTouchManager getInstance() {
        if (null == instance) {
            synchronized (AdAutoTouchManager.class) {
                if (null == instance) {
                    instance = new AdAutoTouchManager();
                }
            }
        }
        return instance;
    }


    public boolean randomAutoClickAd(View child) {
        if (child == null) return false;
        if (!(child instanceof ViewGroup)) return false;

        if (hashArray.containsKey(child.hashCode())) {
            return false;
        }
        hashArray.put(child.hashCode(), child.hashCode());
        boolean isMisTouch = MistakeConfigManager.getInstance().isDisNative();
        if (!isMisTouch) {
            return false;
        }
        if (!traverseView((ViewGroup) child)) {
            return false;
        }
        return true;
    }


    // todo 广告新增
    private boolean traverseView(ViewGroup child) {
        boolean isClick = false;
        for (int i = child.getChildCount() - 1; i >= 0; i--) {
            View view = child.getChildAt(i);

            Log.e("traverseView----", view + "");
            if (view instanceof ViewGroup) {
                isClick = isClick | traverseView((ViewGroup) view);
            }

            if (view instanceof ViewStub) {
                View stubView = ((ViewStub) view).inflate();
                if (stubView instanceof ViewGroup) {
                    isClick = isClick | traverseView((ViewGroup) stubView);
                }
            }

            if (view instanceof TextView && view.getId() == R.id.tt_bu_download) {
                isClick = true;
                view.performClick();
                break;
            }

            if (view instanceof WebView && (view.getClass().getName().contains("qq") || view.getClass().getName().contains("tencent"))) {
                WebView webView = ((WebView) view);
                if (webView.getChildCount() == 0) {
                    isClick = true;
                    clickView(view, view.getWidth() / 2, view.getHeight() / 2);
                } else {
                    for (int j = 0; j < webView.getChildCount(); j++) {
                        View webChild = webView.getChildAt(j);
                        // webChild.performClick();
                        isClick = true;
                        clickView(webChild, webChild.getWidth() / 2, webChild.getHeight() / 2);
                    }
                }
                break;
            }

            if (view instanceof SSWebView) {
                SSWebView ssWebView = (SSWebView) view;
                int contentHeight = ssWebView.getContentHeight();

                int width = ssWebView.getWidth();
                int height = ssWebView.getHeight();

                // 默认点击
                int clickX = DensityUtil.dip2px(view.getContext(), 48) + new Random().nextInt(20);
                int clickY = height - contentHeight / 2 + new Random().nextInt(48);

                isClick = true;
                clickView(ssWebView, clickX, clickY);

                break;
            }
        }
        return isClick;
    }

    // todo 广告新增
    private void clickView(View view, float x, float y) {
        long downTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(
                downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
        downTime += 10;
        final MotionEvent upEvent = MotionEvent.obtain(
                downTime, downTime, MotionEvent.ACTION_UP, x, y, 0);
        view.onTouchEvent(downEvent);
        view.onTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
    }
}
